<template>
	<view>
		<view class="main">
			<u-form :model="form" ref="uForm" label-width="160">
				<u-form-item label="头像">
					<u-avatar :src="src" size="120"></u-avatar>
				</u-form-item>
				<u-form-item label="昵称">
					<u-input v-model="form.nickName"></u-input>
				</u-form-item>
				<u-form-item label="性别">
					<u-radio-group v-model="radio" @change="change">
									<u-radio v-for="(item, index) in radioList" :key="index" :name="item.name" :disabled="item.disabled">
										{{ item.name }}
									</u-radio>
								</u-radio-group>
				</u-form-item>
				<u-form-item label="用户名">
					<u-input v-model="form.userName"></u-input>
				</u-form-item>
				<u-form-item label="手机号">
					<u-input v-model="form.phonenumber"></u-input>
				</u-form-item>
				<u-form-item label="身份证">
					<u-input v-model="form.idCard"></u-input>
				</u-form-item>
				<u-form-item label="邮箱">
					<u-input v-model="form.email"></u-input>
				</u-form-item>
			</u-form>
		</view>
		<button class="bu" style="margin-top: 40rpx;" @click="submit">修改信息</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form:{
					userName: '',
					avatar: '',
					nickName: '',
					password: '',
					repassword: '',
					phonenumber: '',
					email: '',
					idCard: '',
					sex: ''
				},
				src: '',
				radio: '男',
				radioList: [
					{
						name: '男',
						disabled: false
					},
					{
						name: '女',
						disabled: false
					}
				],
				rules:{
					repassword: [
						{
							validator: (rule,value,callback) => {
								if(value === this.form.password){
									callback()
								}else{
									callback(new Error('两次输入不一致'))
								}
							},
							trigger: ['blur','change']
						}
					]
				}
			};
		},
		onLoad() {
			this.getUser()
		},
		methods:{
			change(e){
				this.radio = e
				if(this.radio === '男'){
					this.form.sex = '1'
				}else{
					this.form.sex = '0'
				}
			},
			getUser(){
				this.$request({
					url: '/prod-api/api/common/user/getInfo'
				}).then(res => {
					this.form = res.user
					if(this.form.sex === '1'){
						this.radio = '男'
					}else{
						this.radio = '女'
					}
				})
			},
			submit(){
				this.$request({
					url: '/prod-api/api/common/user',
					method: 'PUT',
					data: {
						...this.form
					}
				}).then(res => {
					if(res.code === 200){
						uni.showToast({
							title:'修改成功'
						})
						setTimeout(() => {
							uni.switchTab({
								url:'/pages/user/user'
							})
						}, 1000)
					}else{
						uni.showToast({
							icon:'error',
							title:res.msg
						})
					}
				})
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		}
	}
</script>

<style lang="scss">
view{
	padding: 20rpx;
	.main{
		padding: 20rpx;
	}
}
</style>
